package com.cgy.springboot.mapper;

import com.cgy.springboot.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Options;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select * from user")
    List<User> findAll();

    @Select("select * from user where username = #{username}")
    User getUserByUsername(String username);
    
    // 通过openId查询用户
    @Select("select * from user where open_id = #{openId}")
    User getUserByOpenId(String openId);
    
    // 更新用户信息
    @Update("update user set username = #{username},gender = #{gender},phone = #{phone}, nickname = #{nickname}, open_id = #{openId},avatar_url = #{avatarUrl}, password_hash = #{passwordHash}, update_time = #{updateTime} where id = #{id}")
    void updateUser(User user);
    
    // 保存新用户
    @Insert("insert into user(username, nickname, open_id, avatar_url, password_hash, create_time, update_time) values(#{username}, #{nickname}, #{openId}, #{avatarUrl}, #{passwordHash}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void saveUser(User user);
    
    // 根据ID查询用户
    @Select("select * from user where id = #{id}")
    User getUserById(Integer id);
}
